package com.ggqq.book.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ggqq.book.entity.Book;
import com.ggqq.book.mapper.BookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 天才小熊猫
 * @since 2021-01-02
 */
@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookMapper bookMapper;

    //输入页数和每页的大小 查询
    @GetMapping("/findAll/{page}/{size}")
    public Page<Book> findAll(@PathVariable("page") Integer page,@PathVariable("size") Integer size){

        Page<Book> objectPage = new Page<>(page,size);
        return (Page<Book>) bookMapper.selectPage(objectPage,null);

    }

    //保存（更新）
    @PostMapping("/save")
    public String save(@RequestBody Book book){
        int result = bookMapper.insert(book);
        if (result != 0 ) {
            return "success";
        } else {
            return "error";
        }
    }

    //根据id进行查询
    @GetMapping("/findById/{id}")
    public Book findById(@PathVariable("id") Integer id){
        return bookMapper.selectById(id);
    }

    //更新
    @PutMapping("/update")
    public String update(@RequestBody Book book){
        int result = bookMapper.insert(book);
        if (result != 0 ) {
            return "success";
        } else {
            return "error";
        }
    }

    //删除
    @DeleteMapping("/deleteById/{id}")
    public void deleteById(@PathVariable("id") Integer id){
        bookMapper.deleteById(id);
    }



}

